package com.android.semita;
import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.List;

import org.json.*;

import com.android.semita.model.Invoice;
import com.android.semita.model.InvoiceItem;

public class InvoiceMapper {
	public InvoiceMapper(){}
	
	public List<Invoice> getInvoiceListFromJSON(String JSONString){
		JSONString =  new StringBuilder("JSON")
						.append("{\"InvoiceHead\":[")
						.append("{\"InvoiceNo\":\"SI-131100001\",\"CustId\":\"10001\",\"CustName\":\"Jack\",\"DeliveryDate\":\"2013-11-18\",\"InvoiceAmount\":\"1000\"},")
						.append("{\"InvoiceNo\":\"SI-131100002\",\"CustId\":\"10002\",\"CustName\":\"Jack2\",\"DeliveryDate\":\"2013-11-18\",\"InvoiceAmount\":\"2000\"},")
						.append("{\"InvoiceNo\":\"SI-131100003\",\"CustId\":\"10003\",\"CustName\":\"Jack3\",\"DeliveryDate\":\"2013-11-18\",\"InvoiceAmount\":\"3000\"},")
						.append("]}")
						.toString();
		
		List<Invoice> invoices = new ArrayList<Invoice>();
		
		
		
		try {
			JSONObject invoiceJSONObject;
			
			invoiceJSONObject = new JSONObject(JSONString);
			JSONArray invoiceItemJSONObjects = invoiceJSONObject.getJSONArray("InvoiceHead");
			
			for(int i=0; i< invoiceItemJSONObjects.length(); i++)
			{
				JSONObject invoiceJSON = invoiceItemJSONObjects.getJSONObject(i);
				Invoice invoice = new Invoice();
				
				invoice.setInvoiceNo(invoiceJSON.getString("InvoiceNo"));				
				invoice.setCustomerId(invoiceJSON.getString("CustId"));
				invoice.setCustomerName(invoiceJSON.getString("CustName"));
				//invoice.setDeliveryDate(invoiceJSON.getString("DeliveryDate"));
				invoice.setInvoiceAmount(Float.parseFloat(invoiceJSON.getString("InvoiceAmount")));
				
				invoices.add(invoice);
				
			}
			
			return invoices;
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return null;
	}

	public Invoice getInvoiceFromJSON(String JSONString){
		Invoice invoice = new Invoice();
		
		try {
			JSONObject invoiceJSONObject;

			invoiceJSONObject = new JSONObject(JSONString).getJSONObject("InvoiceHead");
			
			invoice.setInvoiceNo(invoiceJSONObject.getString(Invoice.INVOICE_NO));
			invoice.setCustomerId(invoiceJSONObject.getString(Invoice.CUSTOMER_ID));
			invoice.setCustomerName(invoiceJSONObject.getString(Invoice.CUSTOMER_NAME));
			invoice.setCustomerAddress(invoiceJSONObject.getString(Invoice.CUSTOMER_ADDRESS));
			
			JSONArray invoiceItemJSONObjects = invoiceJSONObject.getJSONArray("InvoiceItems");
			float total = 0.00F;
			
			for (int i = 0; i < invoiceItemJSONObjects.length(); i++) 
			{
				JSONObject jsonObj = invoiceItemJSONObjects.getJSONObject(i);
				InvoiceItem invoiceItem = new InvoiceItem();

				invoiceItem.setProductID(jsonObj.getString(InvoiceItem.PRODUCT_ID));
				invoiceItem.setProductName(jsonObj.getString(InvoiceItem.PRODUCT_NAME));
				invoiceItem.setQuantity(jsonObj.getInt(InvoiceItem.QUANTITY)); 
				invoiceItem.setItemAmount(jsonObj.getLong(InvoiceItem.PRICE_PER_UNIT));
				invoiceItem.setUnit(jsonObj.getString(InvoiceItem.UNIT));
				invoiceItem.setNetItemAmount(jsonObj.getLong(InvoiceItem.NET_AMOUNT));

				Double amt = jsonObj.getDouble(InvoiceItem.PRICE_PER_UNIT);
				total += amt;

				invoice.Items.add(invoiceItem);
			}
			
			invoice.setInvoiceAmount(total);
			
		} catch (JSONException e) 
		{
			e.printStackTrace();
		}
		
		return invoice;
	}
}
